#!/bin/bash
## test home
testdir=$(cd $(dirname $0)/; pwd)
## word home
workdir=$(dirname $testdir)
cd $workdir

control=./control

cfg=./cfg.json
httpport=80
if [ -f $cfg ]; then
    httpport=`cat $cfg | grep -A3 "\"http\":" | grep "\"listen\"" | cut -d\" -f4 | cut -d: -f2`
fi
httpprex="127.0.0.1:$httpport"

## health
function health(){
    url="/health"
    curl -s "$httpprex$url" && printf "\n"
}

## proc
function proc(){
    args=$@
    for i in $@; do
        url="$url/$i"
    done
    curl -s "$httpprex$url" | python -m json.tool
}

## config
function config(){
    args=$@
    for i in $@; do
        url="$url/$i"
    done
    curl -s "$httpprex$url" | python -m json.tool
}

## control
function control(){
    $control $@
}

## debug
function debug(){
    args=$@
    for i in $@; do
        url="$url/$i"
    done
    curl -s "$httpprex$url" && printf "\n"
}

## pfc
function pfc(){
    args=$@
    for i in $@; do
        url="$url/$i"
    done
    curl -s "$httpprex$url" | python -m json.tool
}

action=$1
case $action in
    "health")
        health
        ;;
    "debug")
        debug $@
        ;;
    "config")
        config $@
        ;;
    "proc")
        proc $@
        ;;
    "pfc")
        pfc $@
        ;;
    "")
        pfc "pfc/proc/metrics/gauge,meter/json"
        ;;
    *)
        control $@
        ;;
esac

